package display.info
{
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import mx.binding.utils.BindingUtils;
	import mx.core.UIComponent;
	
	import spark.primitives.RichEditableText;
	
	import vo.RoomVO;
	
	public class RoomInfo extends UIComponent
	{
		/**
		 * 坐标位置相对于哪个组件
		 */
		private var _relativeWidget:DisplayObject;
		
		/**
		 * 相对relativeWidget的位置
		 */
		private var _reliativeLocation:Point;
		
		private var locationChanged:Boolean;
		
		private var _editable:Boolean;
		
		private var richText:RichEditableText;
		
		private var _infoData:RoomVO;
		
		private var dataChanged:Boolean;
		
		public function RoomInfo()
		{
			super();
		}
		
		protected override  function createChildren() : void
		{
			super.createChildren();
			if(!richText)
			{
				richText = new RichEditableText();
				richText.left = 0;
				richText.bottom = 0;
				richText.right = 0;
				richText.top = 0;
				richText.setStyle("paddingLeft", 3);
				richText.setStyle("paddingTop", 5);
				richText.setStyle("paddingRight", 3);
				richText.setStyle("paddingBottom", 3);
				richText.autoSize = true;
				this.addChild(richText);
			}
		}
		
		protected override function measure():void
	    {
	        super.measure();
	        if(richText)
	        {
	        	 measuredWidth = Math.max(40, richText.contentWidth);
	       		 measuredHeight = Math.max(40, richText.contentHeight);
	       		 this.setActualSize(measuredWidth, measuredHeight);
	        } 
	    }
	    
	    protected override function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number) : void
	    {
	    	refreshLocation();
	    	richText.width = unscaledWidth;
	    	richText.height = unscaledHeight;
    		if(richText.textFlow && richText.textFlow.flowComposer)
			{
				richText.textFlow.flowComposer.updateAllControllers();
			}
	    }
			
		protected override function commitProperties() : void
		{
			super.commitProperties();
			if(locationChanged)
			{
				locationChanged = false;
				this.invalidateDisplayList();
			}
			if(dataChanged)
			{
				richText.content = makeVOContent(this.infoData);
				dataChanged = false;
				this.invalidateSize();
				this.invalidateDisplayList();
			}
		}
		
		private function makeXMLContent(content:XML):String
		{
			if(content == null)
			{
				return "<span> </span>";
			}
			var template:String ="<p textAlign='center' fontFamily='宋体' >*</p>";
			var childs:Array =["name", "size", "id"];
			var length:int = childs.length;
			var string:String = "";
			for(var i:int = 0; i < length; i++)
			{
				var fieldName:String   = childs[i] as String;
				var item:String = fieldName == "id" ? content.@id : content[fieldName];
				switch(fieldName)
				{
					case "name":
					    item = item.replace(" ", "<br></br>");
					    break;
					case "size":
					    item = "AREA: " + item + "㎡";
					    break;
				}
				string += template.replace("*", item);
			}
			return string;
		}
		
		private function makeVOContent(content:RoomVO):String
		{
			if(content == null)
			{
				return "<span> </span>";
			}
			var template:String ="<p textAlign='center' fontFamily='宋体' >*</p>";
			var childs:Array =["name", "size", "id"];
			var length:int = childs.length;
			var string:String = "";
			for(var i:int = 0; i < length; i++)
			{
				var fieldName:String   = childs[i] as String;
				var item:String = fieldName == "id" ? content.id : content[fieldName];
				switch(fieldName)
				{
					case "name":
					    item = item.replace(" ", "<br></br>");
					    break;
					case "size":
					    item = "" + item + "平米";
					    break;
				}
				string += template.replace("*", item);
			}
			return string;
		}

		public function get relativeWidget():DisplayObject
		{
			return _relativeWidget;
		}

		public function set relativeWidget(v:DisplayObject):void
		{
			_relativeWidget = v;
			if(relativeWidget)
			{
				relativeWidget.addEventListener("locationChanged", onWidgetLocationChanged);
			}
			locationChanged = true;
			this.invalidateProperties();
		}
		
		private function onWidgetLocationChanged(event:Event):void
		{
			locationChanged = true;
			this.invalidateProperties();
		}
		
		/**
		 * 重新根据relativeWidget的位置计算该info的位置，如果
		 * reliativeLocation为null，则居中显示该info
		 */
		private function refreshLocation():void
		{
			if(!this.parent || !this.relativeWidget)
			{
				return;
			}
			var x:Number;
			var y:Number;
			var widgetBounds:Rectangle = relativeWidget.getBounds(this.parent);
			if(reliativeLocation)
			{
				x = widgetBounds.x + reliativeLocation.x;
				y = widgetBounds.y + reliativeLocation.y;
			}//居中
			else
			{
				x = widgetBounds.x + (widgetBounds.width - this.width) / 2;
				y = widgetBounds.y + (widgetBounds.height - this.height) / 2;
			}
			this.x = x;
			this.y = y;
		}

		public function get reliativeLocation():Point
		{
			return _reliativeLocation;
		}

		public function set reliativeLocation(v:Point):void
		{
			_reliativeLocation = v;
			locationChanged = true;
			this.invalidateProperties();
		}

		public function get editable():Boolean
		{
			return richText ? richText.editable : _editable;
		}

		public function set editable(v:Boolean):void
		{
			_editable = v;
			if(richText)
			{
				richText.editable = v;
			}
		}

		public function get infoData():RoomVO
		{
			return _infoData;
		}

		public function set infoData(v:RoomVO):void
		{
			_infoData = v;
			dataChanged = true;
			this.invalidateProperties();
		}

	}
}